<?php
/**
 * 
 * The router class decides which controller should recieve control next.
 * This is essentialy the bootstrap router, and it is only concerned with passing along
 * controll to the appropriate controller, however, it also needs to check
 * the requested controller for the existence of the method requested in order to pass a 404 appropriately.
 * @author Martin Sommervold - martin@sommervold.com
 *
 */
namespace TeamPlayers;

class Router {
	
	/**
	 * 
	 * The requested controller from the URL
	 * @var string
	 */
	private $requestedController;
	
	/**
	 * 
	 * The requested method from the URL
	 * @var string
	 */
	private $requestedMethod;

	/**
	 * 
	 * Set by construct through Registry. Holds the name of the controller to be instantiated.
	 * @var string
	 */
	public $assignedController;
	
	/**
	 * 
	 * If a method is provided, this var will hold it if it exists within the assigned controller
	 * @var string
	 */
	public $assignedMethod;
	
	
	/**
	 * 
	 * This is the placeholder for the instantiated controller that the router decides on.
	 * @var object
	 */
	public $loadedController;
	
	/**
	 * 
	 * Holds the classname of the controller to be loaded,.php omitted
	 * @var string
	 */
	public $controllerClassName;
	
	
	/**
	 * 
	 * The Router Construct performs conditional checks to decide on which controller is loaded next.
	 */
	function __construct(){
		
		/*
		 * Get provided information for controller and method
		 */
		
		
		if(!empty($_GET['controller'])){
			$this->requestedController = TeamPlayers::$db->real_escape_string(strtolower($_GET['controller']));
			
		}
		
		if(!empty($_GET['method'])){
			$this->requestedMethod = TeamPlayers::$db->real_escape_string(strtolower($_GET['method']));
		}
		
			
		// No controller requested, provide default
		if(empty($this->requestedController) || $this->requestedController == DefaultController){
			$this->assignedController = DefaultController;	
		}
		
		
		// A controller is provided, check and launch
		if(!empty($this->requestedController)  && is_file("controllers/".$this->requestedController.".controller.php")){
			// The controller exists
			$this->assignedController = $this->requestedController;
			
		} 
		
		// Check if the requested controller corresponds with an actuall file in the folders dir
		if(!is_file("controllers/".$this->assignedController.".controller.php")) {
			$this->display404("requested controller not found on disk");
			die();
		}
		
		
		// A controller has been decided on, and found on disk. Attempt to load it.
		require_once("controllers/".strtolower($this->assignedController).".controller.php");
		
		// Controller file loaded, check if it has a classname as expected (this name should be nameController).
		if(!class_exists("\TeamPlayers\\".$this->assignedController."Controller")){
			$this->display404("class name wrong");
			die();
		}
		
		// Our selected controller is loaded. If a method is requested as well, check if it's there. 404 if not.
		if(!empty($this->requestedMethod)){
			
			if(method_exists("\TeamPlayers\\".$this->assignedController."Controller", $this->requestedMethod)){
				$this->assignedMethod = $this->requestedMethod;
				
			} else {
				$this->display404("method does not exist. Requested method: $this->requestedMethod");
				
				die();
			}
		}
		
		// Everything is decided on, load our controller!
		$this->controllerClassName = "\TeamPlayers\\".$this->assignedController."Controller";
		$this->loadedController = new $this->controllerClassName();

	}
	

	public function display404($reason = false){
		if(!empty($reason)){
			TeamPlayers::$smarty->assign("reason404", $reason);
		}
		TeamPlayers::$smarty->display("404.tpl");
		die();
	}
}

